//
//  AddPlanViewController.m
//  Wallets Manager
//
//  Created by Tung Nguyen on 10/7/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "AddPlanViewController.h"

@interface AddPlanViewController ()

@end

@implementation AddPlanViewController
@synthesize addPlanTableView;
@synthesize addButton;
@synthesize titleLabel;
@synthesize planNameTextField;
@synthesize goalTextField;
@synthesize startAmountTextField;
@synthesize categoryImv;
@synthesize walletLabel;
//@synthesize isAdded;
@synthesize formKind;
@synthesize currentPlan;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
   
    
    //----------------------Init Current Wallet Name And Category Icon-------------------//
    folyAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    walletLabel.text = delegate.currentWallet.w_name;
    currentImageName = kCategoryIcon;
    
    // Do any additional setup after loading the view from its nib.
    //-----------------------Transform View to Edit View----------------------//
    if ([formKind isEqualToString:@"Edit Form"]){
        addButton.hidden = YES;
        titleLabel.text = @"Edit Campain";
        
        //Init editPlanButton 
        editPlanButton = [UIButton buttonWithType:UIButtonTypeCustom];
        editPlanButton.frame = addButton.frame;
        editPlanButton.backgroundColor = [UIColor clearColor];
        editPlanButton.font = [UIFont boldSystemFontOfSize:14];
        [editPlanButton setTitle:@"Edit" forState:UIControlStateNormal];
        [editPlanButton setBackgroundImage:[UIImage imageNamed:@"filter_btn.png"] forState:UIControlStateNormal];
        [editPlanButton addTarget:self action:@selector(editButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:editPlanButton];
        
        // Init text field with old plan
        planNameTextField.text = currentPlan.p_name;
        goalTextField.text = [NSString stringWithFormat:@"%d",[currentPlan.p_amount intValue]];
        startAmountTextField.text = [NSString stringWithFormat:@"%d",[currentPlan.p_startamount intValue]];
        [categoryImv setImage:[UIImage imageNamed:currentPlan.p_image]];
         currentImageName = currentPlan.p_image;
    }
    
    //---------------------------NSNotification Center ------------------------//
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPlanImageOnSuccess:) name:@"getPlanImageOnSuccess" object:nil];
}

- (void)viewDidUnload
{
    [self setAddPlanTableView:nil];
    [self setPlanNameTextField:nil];
    [self setGoalTextField:nil];
    [self setStartAmountTextField:nil];
    [self setWalletLabel:nil];
    [self setCategoryImv:nil];
    [self setAddButton:nil];
    [self setTitleLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
//    return 3;
//}
//
//- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if(section == 0){
//        return 1;
//    }
//    return 2;
//}
//
//-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 60;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 15;
//}
//
//- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *cellInditifer = @"Cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInditifer];
//    
//    UILabel *titleLabel;
//    UITextField *contentTextField;
//    
//    if( cell == nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInditifer];
//        cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@""]];
//        
//        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 100, 50)];
//        titleLabel.tag = 100;
//        [cell addSubview:titleLabel];
//        
//        contentTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 185, 60)];
//        contentTextField.borderStyle = UITextBorderStyleNone;
//        contentTextField.tag = 200;
//        [cell addSubview:contentTextField];
//        
//    }
//}

#pragma mark - TextField Delegate

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - Touch Delegate {

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:touch.view];
    
    if( touchPoint.y >=271 && touchPoint.y <= 321) {
        ChooseWalletIconViewController *chooseIconViewController = [[ChooseWalletIconViewController alloc]init];
        chooseIconViewController.fromView = @"AddPlanView";
        [self.navigationController pushViewController:chooseIconViewController animated:YES];
    }
    
}

#pragma mark - NSNotification 
-  (void) getPlanImageOnSuccess : (NSNotification *) notification {
    NSDictionary *tempDict = [notification object];
    currentImageName = [tempDict objectForKey:@"key"];
    [categoryImv setImage:[UIImage imageNamed:currentImageName]];
}

#pragma mark - IBAction Delegate
- (IBAction)addButtonPressed:(id)sender {
    
    folyAppDelegate *delegate = (folyAppDelegate *)[[UIApplication sharedApplication]delegate];
    
    [DataBaseAccess createPlanWithName:planNameTextField.text andAmount:[goalTextField.text doubleValue] andstartAmout:[startAmountTextField.text doubleValue] andImage:currentImageName inWallet:delegate.currentWallet andIsCompleted: NO];
    
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadPlanTableView" object:nil];
}

- (IBAction)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Button Event
- (void) editButtonPressed {
    currentPlan.p_name = planNameTextField.text;
    currentPlan.p_amount = [NSNumber numberWithDouble:[goalTextField.text doubleValue]];
    currentPlan.p_startamount = [NSNumber numberWithDouble:[startAmountTextField.text doubleValue]];
    currentPlan.p_image = currentImageName;

    [DataBaseAccess saveData];
    [self.navigationController popViewControllerAnimated:YES];
    
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadPlanTableView" object:nil];
}
@end
